﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.HtmlControls;

public partial class Admin_Login : System.Web.UI.Page
{
    public HtmlGenericControl divWarning;

    protected void Page_Load(object sender, EventArgs e)
    {

        divWarning = Login1.FindControl("divWarning") as HtmlGenericControl;
        if (!string.IsNullOrEmpty(Common.CurrentUserName))
            Response.Redirect("index.aspx");

    }

    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
        //Update status for user login complete
        //try
        //{
        //    //UsersEntity user = UsersManager.CreateInstant().SelectByUserNameLST(Login1.UserName)[0];
        //    //user.LastLoginDate = user.LastLockoutDate;
        //    //user.LastLockoutDate = DateTime.Now;
        //    //UsersManager.CreateInstant().Update(user);
        //}
        //catch { }
        //if (!string.IsNullOrEmpty(Request["ReturnUrl"]))
        //{
        //    Response.Redirect(Request["ReturnUrl"]);
        //}
        //else
        //    Response.Redirect(PublicContants.PageIndex);
    }
    protected void Login1_LoginError(object sender, EventArgs e)
    {
        //Response.Redirect("index.aspx");
        divWarning.Visible = true;
        System.Web.UI.WebControls.Login log = (System.Web.UI.WebControls.Login)sender;
        string user = log.UserName;
        string pass = log.Password;

        if (Membership.FindUsersByName(user).Count <= 0)
            Login1.FailureText = "Tài khoản không tồn tại";
        else
        {
            MembershipUser u = Membership.GetUser(user);
            if (!Membership.ValidateUser(user, pass))
            {
                if (!u.IsApproved)
                    Login1.FailureText = "Tài khoản đã bị khóa";
                else
                    Login1.FailureText = "Mật khẩu không đúng";
            }
        }
    }

    protected void checkValidServer_ServerValidate(object source, ServerValidateEventArgs args)
    {
        //args.IsValid = true;
        //divWarning.Visible = true;
        //string user = Login1.UserName;
        //string pass = Login1.Password;
        //CustomValidator checkValidServer = Login1.FindControl("checkValidServer") as CustomValidator;

        //if (!string.IsNullOrEmpty(user))
        //{
        //    args.IsValid = false;
        //    checkValidServer.ErrorMessage = "Tên quyền này đã tồn tại";
        //    Login1.Focus();
        //}
    }
}